//package com.coolw.word2pdf.util;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.poi.hwpf.HWPFDocument;
//import org.apache.poi.hwpf.converter.WordToHtmlConverter;
//import org.apache.poi.xwpf.converter.core.FileImageExtractor;
//import org.apache.poi.xwpf.converter.core.FileURIResolver;
//import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
//import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
//import org.apache.poi.xwpf.usermodel.XWPFDocument;
//import org.w3c.dom.Document;
//
//import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.parsers.ParserConfigurationException;
//import javax.xml.transform.OutputKeys;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerException;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
//import java.io.*;
//
///**
// * word转html工具类
// *
// * @author coolw
// * @date 2023/10/23 19:47
// */
//
//@Slf4j
//public class WordToHtmlUtils {
//      //https://blog.csdn.net/zch981964/article/details/132827451
//    /**
//     * 2007版本word转换成html
//     */
//
//    public static void word2007ToHtml() throws IOException {
//        String filepath = "C:/test/";
//        String fileName = "test.docx";
//        String htmlName = "test.html";
//        final String file = filepath + fileName;
//        File f = new File(file);
//        if (!f.exists()) {
//            System.out.println("Sorry File does not Exists!");
//        } else {
//            if (f.getName().endsWith(".docx")) {
//
//                // 1) 加载word文档生成 XWPFDocument对象
//                InputStream in = new FileInputStream(f);
//                XWPFDocument document = new XWPFDocument(in);
//
//                // 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
//                File imageFolderFile = new File(filepath);
//                XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
//                options.setExtractor(new FileImageExtractor(imageFolderFile));
//                options.setIgnoreStylesIfUnused(false);
//                options.setFragment(true);
//
//                // 3) 将 XWPFDocument转换成XHTML
//                OutputStream out = new FileOutputStream(filepath + htmlName);
//                XHTMLConverter.getInstance().convert(document, out, options);
//
//                //也可以使用字符数组流获取解析的内容
////                ByteArrayOutputStream baos = new ByteArrayOutputStream();
////                XHTMLConverter.getInstance().convert(document, baos, options);
////                String content = baos.toString();
////                System.out.println(content);
////                 baos.close();
//            } else {
//                throw new RuntimeException("word文档必须以.docx结尾");
//            }
//        }
//    }
//
//    /**
//     * 2003版本word转换成html
//     */
//
//    public static void word2003ToHtml() throws IOException, TransformerException, ParserConfigurationException {
//        String filepath = "D:/test/";
//        final String imagepath = "C:/test/image/";
//        String fileName = "test.doc";
//        String htmlName = "test.html";
//        final String file = filepath + fileName;
//
//        InputStream input = new FileInputStream(file);
//        HWPFDocument wordDocument = new HWPFDocument(input);
//        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//        //设置图片存放的位置
//        wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> {
//            File imgPath = new File(imagepath);
//            if (!imgPath.exists()) {//图片目录不存在则创建
//                imgPath.mkdirs();
//            }
//            File file1 = new File(imagepath + suggestedName);
//            try {
//                OutputStream os = new FileOutputStream(file1);
//                os.write(content);
//                os.close();
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return imagepath + suggestedName;
//        });
//
//        //解析word文档
//        wordToHtmlConverter.processDocument(wordDocument);
//        Document htmlDocument = wordToHtmlConverter.getDocument();
//
//        File htmlFile = new File(filepath + htmlName);
//        OutputStream outStream = new FileOutputStream(htmlFile);
//
//        //也可以使用字符数组流获取解析的内容
////        ByteArrayOutputStream baos = new ByteArrayOutputStream();
////        OutputStream outStream = new BufferedOutputStream(baos);
//
//        DOMSource domSource = new DOMSource(htmlDocument);
//        StreamResult streamResult = new StreamResult(outStream);
//
//        TransformerFactory factory = TransformerFactory.newInstance();
//        Transformer serializer = factory.newTransformer();
//        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
//        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
//        serializer.setOutputProperty(OutputKeys.METHOD, "html");
//
//        serializer.transform(domSource, streamResult);
//
//        //也可以使用字符数组流获取解析的内容
////        String content = baos.toString();
////        System.out.println(content);
////        baos.close();
//        outStream.close();
//    }
//}
//
